package imv3.foo.client.handler;

import imv3.protocol.response.LoginResponsePacket;
import imv3.protocol.session.Session;
import imv3.util.SessionUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019-04-25 22:04
 * 既然选择了远方,便只顾风雨兼程
 */
public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponsePacket> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket loginResponsePacket) throws Exception {
        String userId = loginResponsePacket.getUserId();
        String username = loginResponsePacket.getUsername();

        if (loginResponsePacket.isSuccess()) {
            System.out.println("[" + username + "]登录成功, userId为: " + loginResponsePacket.getUserId());
            SessionUtil.bind(new Session(userId, username), ctx.channel());
        } else {
            System.out.println("[" + username + "]登录失败, 原因是: " + loginResponsePacket.getReason());
        }
    }
}
